package com.open.capacity.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: y_022
 * @date: 15:41-2021/8/13
 * @description:
 */
@Component
public class SendMessage implements ChannelAwareMessageListener {
    private RabbitAdmin rabbitAdmin;

    @Autowired
    private void setRabbitAdmin(RabbitAdmin rabbitAdmin) {
        this.rabbitAdmin = rabbitAdmin;
    }

    private RabbitTemplate rabbitTemplate;

    @Autowired
    private void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }


//    public void createQueueAndExchangeAndBindExchange(String queueName, String exchangeName, String routingkey, String msg) {
//        //创建交换机，存在则直接使用
//        DirectExchange directExchange = new DirectExchange(exchangeName, true, false);
//        rabbitAdmin.declareExchange(directExchange);
//
//        //创建队列，存在则直接使用
//        Queue queue = new Queue(queueName, true, false, false);
//        rabbitAdmin.declareQueue(queue);
//
//        //队列和交换机绑定
//        Binding with = BindingBuilder.bind(queue).to(directExchange).with(routingkey);
//        rabbitAdmin.declareBinding(with);
//
//        //封装消息
//    public void createQueueAndExchangeAndBindExchange(String queueName, String exchangeName, String routingkey, String msg) {
//        //创建交换机，存在则直接使用
//        DirectExchange directExchange = new DirectExchange(exchangeName, true, false);
//        rabbitAdmin.declareExchange(directExchange);
//
//        //创建队列，存在则直接使用
//        Queue queue = new Queue(queueName, true, false, false);
//        rabbitAdmin.declareQueue(queue);
//
//        //队列和交换机绑定
//        Binding with = BindingBuilder.bind(queue).to(directExchange).with(routingkey);
//        rabbitAdmin.declareBinding(with);
//
//        //封装消息
//        Message message = new Message(msg.getBytes(), new MessageProperties());
//        System.out.println(message);
//
//        //发送消息
//        rabbitTemplate.send(exchangeName, routingkey, message);
//
//    }

//        Message message = new Message(msg.getBytes(), new MessageProperties());
//        System.out.println(message);
//
//        //发送消息
//        rabbitTemplate.send(exchangeName, routingkey, message);
//
//    }

    public void createQueueAndExchangeAndBindExchange(String queueName, String exchangeName, String routingkey, String msg) {
        String deadRoutingKey = "dlxRoutingKey";
        //创建正常交换机，存在则直接使用
        DirectExchange directExchange = new DirectExchange(exchangeName, true, false);
        rabbitAdmin.declareExchange(directExchange);
        //死信交换机
        DirectExchange directDeadExchange = new DirectExchange("Dead-Exhcange", true, false);
        rabbitAdmin.declareExchange(directDeadExchange);

        //创建正常的队列，存在则直接使用
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","Dead-Exhcange");
        arguments.put("x-dead-letter-routing-key",deadRoutingKey);
        Queue queue = new Queue(queueName, true, false, false,arguments);
        rabbitAdmin.declareQueue(queue);

        //创建死信的队列，
        Queue deadQueue = new Queue("Dead-Queue", true, false, false);
        rabbitAdmin.declareQueue(deadQueue);

        //死信交换机绑定死信队列
        Binding with1 = BindingBuilder.bind(deadQueue).to(directDeadExchange).with(deadRoutingKey);
        rabbitAdmin.declareBinding(with1);


        //正常队列和正常交换机绑定
        Binding with = BindingBuilder.bind(queue).to(directExchange).with(routingkey);
        rabbitAdmin.declareBinding(with);

        //封装消息
        Message message = new Message(msg.getBytes(), new MessageProperties());
        System.out.println(message);

        //发送消息
        rabbitTemplate.convertAndSend(exchangeName, routingkey, message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException { //消息的后处理
                //1.设置message的过期时间，
                message.getMessageProperties().setExpiration("5000");
                return message;
            }
        });
    }

    public String GetMessage(String queueName) {
        Message receive = rabbitTemplate.receive(queueName);//通过队列名来获取消息
        //按道理消息接收之后应该存在数据库中，但是这里暂时未做业务处理
        long deliveryTag = receive.getMessageProperties().getDeliveryTag();
//        try{
        //1接收消息
        System.out.println(receive.getBody());
        //2.处理业务逻辑
//        }
        return new String(receive.getBody());
    }

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            System.out.println(new String(message.getBody()));
            channel.basicAck(deliveryTag, true);
        } catch (Exception e) {
            channel.basicAck(deliveryTag, false);
        }
    }


}

